// pages/needList/needList.js
const MAX_LIMIT = 4
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allList: [],
    list: [],
    endList: [],
    total1: -1,
    total2: -1,
    total3: -1,
    searchKey: '',
    status:'-1',//-1全部  0 进行中 1 已完成
    active: 0,
    noMore1: false,
    noMore2: false,
    noMore3: false,
    searchKeyList:['','',''],
    option: [
      { text: '全部', value: 0 },
      { text: '元宇宙', value: 1 },
      { text: '游戏配音', value: 2 },
      { text: '有声书', value: 3 },
      { text: '动漫配音', value: 4 },
    ],
    needType: 0,
  },
  onSearch(e){
    console.log(e.detail)
    const active = this.data.active
    const searchKeyList = this.data.searchKeyList
    searchKeyList[active]=e.detail
    this.setData({
      searchKeyList,
      searchKey:e.detail
    })
    this.initList(this.data.status,this.data.needType,this.data.searchKey)
  },

  onCancel(){
    const active = this.data.active
    const searchKeyList = this.data.searchKeyList
    searchKeyList[active]=''
    this.setData({
      searchKeyList,
      searchKey:''
    })
    this.initList(this.data.status,this.data.needType,this.data.searchKey)
  },

  onDropChange(e){
    console.log(e.detail)
    this.setData({
      needType:e.detail
    })
    this.initList(this.data.status,this.data.needType,this.data.searchKey)
  },
 
  getList(status,needType=0,searchKey='') {
    wx.showLoading({
      title: '加载中',
    })
    let listLen = 0
    switch (status) {
      case '-1':
        listLen = this.data.allList.length
        break;
      case '0':
        listLen = this.data.list.length
        break;
      case '1':
        listLen = this.data.endList.length
        break;
      default:
        break;
    }
    wx.cloud.callFunction({
      name: 'getNeed',
      data: {
        $url: 'list',
        status: status,
        searchKey:searchKey,
        needType:needType,
        start:listLen,
        count: MAX_LIMIT
      },
    }).then(res => {
      console.log(res)
      switch (status) {
        case '-1':
          this.setData({
            allList: this.data.allList.concat(res.result.list.list),
          })
          break;
        case '0':
          this.setData({
            list: this.data.list.concat(res.result.list.list),
          })
          break;
        case '1':
          this.setData({
            endList: this.data.endList.concat(res.result.list.list)
          })
          break;
        default:
          break;
      }  
      // wx.stopPullDownRefresh()
      wx.hideLoading()
    })
  },
  
  tabChange(e) {
    const active = e.detail.index
    const searchKeyList = this.data.searchKeyList
    const searchKey = searchKeyList[active]
    console.log(active)
    console.log(searchKeyList)
    console.log(searchKey)
    this.setData({
      active,
      searchKey
    })
    switch (active) {
      case 0:
        this.setData({
          status:'-1'
        })
        break;
      case 1:
        this.setData({
          status:'0'
        })
        if(this.data.total2==-1){
          this.initList('0',this.data.needType)
        }
        break;
      case 2:
        this.setData({
          status:'1'
        })
        if(this.data.total3==-1){
          this.initList('1',this.data.needType)
        }
        break;
      default:
        break;
    }
  },

  initList(status,needType=0,searchKey=''){
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'getNeed',
      data: {
        $url: 'list',
        searchKey:searchKey,
        status:status,
        needType:needType,
        start: 0,
        count: MAX_LIMIT
      },
    }).then(res => {
      console.log(res)
      switch (status) {
        case '-1':
          this.setData({
            allList:res.result.list.list,
            total1: res.result.total.total
          })
          break;
        case '0':
          this.setData({
            list:res.result.list.list,
            total2: res.result.total.total
          })
          break;
        case '1':
          this.setData({
            endList:res.result.list.list,
            total3: res.result.total.total
          })
          break;
        default:
          break;
      }   
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const needType = wx.getStorageSync('needType')
    if(needType){
      this.setData({
        needType
      })
    }
    this.initList('-1',this.data.needType)
    // this.initList('0')
    // this.initList('1')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.setData({
    //   list: []
    // })
    // this.getList()  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    switch (this.data.active) {
      case 0:
        if (this.data.allList.length < this.data.total1) {
          this.getList(this.data.status,this.data.needType,this.data.searchKey)
        } else {
          this.setData({
            noMore1: true
          })
        }
        break;
      case 1:
        if (this.data.list.length < this.data.total2) {
          this.getList(this.data.status,this.data.needType,this.data.searchKey)
        } else {
          this.setData({
            noMore2: true
          })
        }
        break;
      case 2:
        if (this.data.endList.length < this.data.total3) {
          this.getList(this.data.status,this.data.needType,this.data.searchKey)
        } else {
          this.setData({
            noMore3: true
          })
        }
        break;
      default:
        break;
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})